package com.dom.domweb.dao.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dom.common.constants.ConstantError;
import com.dom.common.enums.PubStatEnum;
import com.dom.common.model.vo.basic.*;
import com.dom.domweb.common.RspModel;
import com.dom.domweb.dao.entity.TGoodsInfo;
import com.dom.domweb.dao.mapper.TGoodsInfoMapper;
import com.dom.domweb.dao.service.ITGoodStandardTblService;
import com.dom.domweb.dao.service.ITGoodVarietyTblService;
import com.dom.domweb.dao.service.ITGoodsInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dom.domweb.dao.service.ITMeasureUnitTblService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * B_商品信息 服务实现类
 * </p>
 *
 * @author xllei
 * @since 2018-11-17
 */
@Service
public class TGoodsInfoServiceImpl extends ServiceImpl<TGoodsInfoMapper, TGoodsInfo> implements ITGoodsInfoService {

    @Autowired
    private ITGoodStandardTblService standardTblService;
    @Autowired
    private ITGoodVarietyTblService varietyTblService;
    @Autowired
    private ITMeasureUnitTblService unitTblService;

    @Override
    public B149A getGoodsInfoList(B149R request) {
        Wrapper<TGoodsInfo> wrapper = new EntityWrapper();
        Page<TGoodsInfo> page = new Page<>();
        page.setCurrent(request.getPn());
        page.setSize(request.getPs());
        page = this.selectPage(page,wrapper);
        B149A result = new B149A();
        result.setTotalNum((int)page.getTotal());
        Map<String,Object> standard = getAllStandard();
        Map<String,Object> unit = getAllUnit();
        Map<String,Object> variety = getAllVariety();
        List<GoodsInfo> list = infoToTbl(page.getRecords(),standard,unit,variety);
        result.setGoodsData(list);
        return result;
    }

    private List<GoodsInfo> infoToTbl(List<TGoodsInfo> list,Map<String,Object> standard,Map<String,Object> unit, Map<String,Object> variety) {
        List<GoodsInfo> result = new ArrayList<>();
        for (TGoodsInfo tblInfo:  list) {
            GoodsInfo info = new GoodsInfo();
            info.setId(tblInfo.getId().toString());
            info.setMemCode(tblInfo.getMemCode());
            info.setStat(tblInfo.getStat().toString());
            info.setGoodsCode(tblInfo.getGoodCode());
            info.setGoodsName(tblInfo.getGoodName());
            info.setStandardCode(tblInfo.getStandardCode());
            info.setStandardName(tblInfo.getStandardCode() == null ? "" : standard.get(tblInfo.getStandardCode().toString()).toString());
            info.setVarietyName(tblInfo.getVarietyCode() == null ? "" : variety.get(tblInfo.getVarietyCode().toString()).toString());
            info.setVarietyCode(tblInfo.getVarietyCode());
            info.setUnitCode(tblInfo.getUnitCode());
            info.setUnit(tblInfo.getUnitCode() == null ? "" : unit.get(tblInfo.getUnitCode().toString()).toString());
            info.setStatName(PubStatEnum.getName(tblInfo.getStat()));
            info.setTaxRate(tblInfo.getTaxRate() == null ? null : tblInfo.getTaxRate().toString());
            result.add(info);
        }
        return result;
    }

    @Override
    public B149A getAllGoodsInfo() {
        B149A b = new B149A();
        Wrapper<TGoodsInfo> wrapper = new EntityWrapper();
        List<TGoodsInfo> list = this.baseMapper.selectList(wrapper);
        Map<String,Object> standard = getAllStandard();
        Map<String,Object> unit = getAllUnit();
        Map<String,Object> variety = getAllVariety();
        List<GoodsInfo> result = infoToTbl(list,standard,unit,variety);
        b.setGoodsData(result);
        return b;
    }

    private Map<String,Object> getAllUnit(){
        Map<String,Object> map = unitTblService.getAllUnitInMap();
        return map;
    }

    private String getUnit(String unitCode){
        return getAllUnit().get(unitCode) == null ? "" : getAllUnit().get(unitCode).toString();
    }

    private Map<String,Object> getAllVariety(){
        Map<String,Object> map = varietyTblService.getAllVarietyInMap();
        return map;
    }

    private String getVarietyName(String varietyCode){
        return getAllVariety().get(varietyCode) == null ? "" : getAllVariety().get(varietyCode).toString();
    }

    private Map<String,Object> getAllStandard(){
        Map<String,Object> map = standardTblService.getAllStandardInMap();
        return map;
    }

    private String getStandardName(String standardCode){
        Map<String,Object> map = getAllStandard();
        return map.get(standardCode) == null ? "" : map.get(standardCode).toString();
    }

    @Override
    public B143A getGoodsInfoByCode(String goodsCode) {
        TGoodsInfo tbl = new TGoodsInfo();
        tbl.setGoodCode(Integer.valueOf(goodsCode));
        tbl =  this.baseMapper.selectOne(tbl);
        B143A result = new B143A();
        result.setId(tbl.getId().toString());
        result.setMemCode(tbl.getMemCode());
        result.setStat(tbl.getStat().toString());
        result.setGoodsCode(tbl.getGoodCode() == null ? null : tbl.getGoodCode().toString());
        result.setGoodsName(tbl.getGoodName());
        result.setStandardCode(tbl.getStandardCode() == null ? null : tbl.getStandardCode().toString());
        result.setStandardName(tbl.getStandardCode() == null ? "" : getStandardName(tbl.getStandardCode().toString()));
        result.setVarietyCode(tbl.getVarietyCode() == null ? null : tbl.getVarietyCode().toString());
        result.setVarietyName(tbl.getVarietyCode() == null ? "" : getVarietyName(tbl.getVarietyCode().toString()));
        result.setUnitCode(tbl.getUnitCode() == null ? null : tbl.getUnitCode().toString());
        result.setUnit(tbl.getUnitCode() == null ? "" : getUnit(tbl.getUnitCode().toString()));
        result.setTaxRate(tbl.getTaxRate() == null ? null : tbl.getTaxRate().toString());
        return result;
    }

    @Override
    public RspModel insertGoodsInfo(B140R request) {
        Wrapper<TGoodsInfo> wrapper = new EntityWrapper<TGoodsInfo>();
        wrapper.eq("good_code",request.getGoodsCode());
        List<TGoodsInfo> list = this.baseMapper.selectList(wrapper);
        if (list != null && list.size() != 0){
            return new RspModel("RSP000032","商品单位已存在！");
        }
        TGoodsInfo tblInfo = new TGoodsInfo();
        tblInfo.setStat(PubStatEnum.STAT_UNACTIVATED.getCode());
        tblInfo.setMemCode(0);
        tblInfo.setUnitCode(request.getUnitCode() == null ? null : Integer.valueOf(request.getUnitCode()));
        tblInfo.setGoodCode(request.getGoodsCode());
        tblInfo.setStandardCode(request.getStandardCode());
        tblInfo.setGoodName(request.getGoodsName());
        tblInfo.setVarietyCode(request.getVarietyCode());
        tblInfo.setTaxRate(StringUtils.isEmpty(request.getTaxRate()) ? null : new BigDecimal(request.getTaxRate()));
        tblInfo.setVersion(0);
        int count = this.baseMapper.insert(tblInfo);
        RspModel model = new RspModel("RSP000000","商品信息新增成功！");
        if (count != 1){
            model = new RspModel(ConstantError.TRADEERROR);
        }
        return model;
    }

    @Override
    public RspModel editGoodsInfo(B140R request) {
        Wrapper<TGoodsInfo> wrapper = new EntityWrapper<TGoodsInfo>();
        wrapper.eq("id",request.getId());
        TGoodsInfo tbl = this.baseMapper.selectById(request.getId());
        if (tbl.getStat() != PubStatEnum.STAT_UNACTIVATED.getCode()
                &&  !request.getGoodsCode().equals(Integer.toString(tbl.getGoodCode())) ){
            return new RspModel(ConstantError.NOT_CODE_ERROR);
        }
        TGoodsInfo tblInfo = new TGoodsInfo();
        tblInfo.setVarietyCode(request.getVarietyCode());
        tblInfo.setUnitCode(request.getUnitCode() == null ? null : Integer.valueOf(request.getUnitCode()));
        tblInfo.setGoodName(request.getGoodsName());
        tblInfo.setGoodCode(request.getGoodsCode());
        tblInfo.setStandardCode(request.getStandardCode());
        tblInfo.setTaxRate(StringUtils.isEmpty(request.getTaxRate()) ? null : new BigDecimal(request.getTaxRate()));
        tblInfo.setStat(StringUtils.isEmpty(request.getStat()) ? PubStatEnum.STAT_UNACTIVATED.getCode() : Integer.valueOf(request.getStat()));
        int count = this.baseMapper.update(tblInfo,wrapper);
        RspModel model = new RspModel("RSP000000","商品信息修改成功！");
        if (count != 1){
            model = new RspModel(ConstantError.TRADEERROR);
        }
        return model;
    }

    @Override
    public RspModel goodsInfoDel(String id) {
        Wrapper<TGoodsInfo> wrapper = new EntityWrapper<TGoodsInfo>();
        wrapper.eq("id",id);
        TGoodsInfo tblInfo = new TGoodsInfo();
        tblInfo.setStat(PubStatEnum.STAT_STOP.getCode());
        int count = this.baseMapper.update(tblInfo,wrapper);
        RspModel model = new RspModel("RSP000000","商品信息删除成功！");
        if (count != 1){
            model = new RspModel(ConstantError.TRADEERROR);
        }
        return model;
    }
}
